@ECHO OFF
REM DONT_STOP is a search string variable to match services to NOT be stopped.
REM A backslash "\" must be placed before any space within a name, otherwise it
REM will be interepreted as a search string seperator. REM This is a "feature"
REM of FINDSTR, and not a limitation of this script.

SETLOCAL
SET DONT_STOP=Audio Client Error Event Netlogon Network Terminal Workstation DameWare LogMeIn NetOp VNC

ECHO.
ECHO [TARS] Toggle All Running Services 7.0
ECHO.===============================================================================
ECHO.

:CHOICE
IF /I "%1" EQU "stop"    GOTO :STOP_SERVICES
IF /I "%1" EQU "start"   GOTO :START_SERVICES
IF /I "%1" EQU "restart" GOTO :RESTART_SERVICES
GOTO :NOOP

:RESTART_SERVICES
CALL :STOP_SERVICES
CALL :START_SERVICES
GOTO :EOF

:STOP_SERVICES
NET START > %TEMP%\TARS1.txt
IF EXIST %TEMP%\TARS1.txt FINDSTR /V /C:"The command completed successfully." %TEMP%\TARS1.txt > %TEMP%\TARS2.txt
IF EXIST %TEMP%\TARS2.txt FINDSTR /V "%DONT_STOP%" %TEMP%\TARS2.txt > %TEMP%\TARS3.txt
IF EXIST %TEMP%\TARS3.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS3.txt) DO ECHO * Stopping %%a... & START /MIN /WAIT NET STOP "%%a" /Y
NET START > %TEMP%\TARS3.txt
IF EXIST %TEMP%\TARS3.txt FINDSTR /V /C:"The command completed successfully." %TEMP%\TARS3.txt > %TEMP%\TARS4.txt
ECHO.
IF EXIST %TEMP%\TARS4.txt ECHO The following services could not be stopped:
IF EXIST %TEMP%\TARS4.txt ECHO.
IF EXIST %TEMP%\TARS4.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS4.txt) DO ECHO %%a
GOTO :END

:START_SERVICES
IF EXIST %TEMP%\TARS2.txt GOTO SERVICE_LIST_1
IF EXIST %TEMP%\SDBO2.txt GOTO SERVICE_LIST_2
GOTO :END

:SERVICE_LIST_1
IF EXIST %TEMP%\TARS2.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS2.txt) DO ECHO * Starting %%a... & START /MIN /WAIT NET START "%%a" /Y
IF EXIST %TEMP%\TARS1.txt DEL %TEMP%\TARS1.txt > NUL:
IF EXIST %TEMP%\TARS2.txt DEL %TEMP%\TARS2.txt > NUL:
IF EXIST %TEMP%\TARS3.txt DEL %TEMP%\TARS3.txt > NUL:
IF EXIST %TEMP%\TARS4.txt DEL %TEMP%\TARS4.txt > NUL:
GOTO :EOF

:SERVICE_LIST_2
IF EXIST %TEMP%\SDBO2.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\SDBO2.txt) DO ECHO * Starting the %%a service.. & START /MIN /WAIT NET START "%%a" /Y
IF EXIST %TEMP%\SDBO1.txt DEL %TEMP%\SDBO1.txt > NUL:
IF EXIST %TEMP%\SDBO2.txt DEL %TEMP%\SDBO2.txt > NUL:
IF EXIST %TEMP%\SDBO4.txt DEL %TEMP%\SDBO4.txt > NUL:
IF EXIST %TEMP%\SDBO3.txt DEL %TEMP%\SDBO3.txt > NUL:
GOTO :EOF

:START_SERVICES_SERVICES
ECHO * Starting %%a .. & START /MIN /WAIT NET START "%%a" /Y
GOTO :EOF

:STOP_SERVICES_SERVICES
ECHO * Stopping %%a .. & START /MIN /WAIT NET STOP "%%a" /Y
GOTO :EOF

:NOOP
ECHO ERROR: No option specified.
ECHO.
ECHO A command-line parameter must be passed in order for this script to fucntion.
ECHO Please use one of the follow command-line options:
ECHO.
ECHO    TARS stop        : Stop all running services.
ECHO    TARS start       : Start services previous stopped.
ECHO    TARS restart     : Stop and restart running services.
GOTO :END

:END
ECHO.
ECHO.===============================================================================
ECHO.
REM [TARS] Toggle All Running Services 7.0
REM Copyright 1998-*, Santeria Systems 
REM Micheal Espinola Jr (michealespinola@gmail.com)
REM
REM This script is free to USE & MODIFY or REDISTRIBUTE in its' ORIGINAL FORM,
REM as long as this REM'd passage is always included.
ENDLOCAL

:EOF
